﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace HYCodeBase.XmlUtility
{
	internal class TypeXmlAttributeBean
	{
		internal Type UseType
		{
			get
			{
				return this.m_useType;
			}
			set
			{
				this.m_useType = value;
			}
		}

		internal Dictionary<PropertyInfo, XmlPropertyAttribute> UsePropertyDic
		{
			get
			{
				return this.m_usePropertyDic;
			}
			set
			{
				this.m_usePropertyDic = value;
			}
		}

		internal List<PropertyInfo> LstCollectionProperty
		{
			get
			{
				return this.m_lstCollectionProperty;
			}
			set
			{
				this.m_lstCollectionProperty = value;
			}
		}

		internal XmlClassAttribute UseClassAttribute
		{
			get
			{
				return this.m_useClassAttribute;
			}
			set
			{
				this.m_useClassAttribute = value;
			}
		}

		internal TypeXmlAttributeBean(Type inputType)
		{
			this.UseType = inputType;
			this.UsePropertyDic = new Dictionary<PropertyInfo, XmlPropertyAttribute>();
			this.LstCollectionProperty = new List<PropertyInfo>();
			XmlClassAttribute useClassAttribute = this.UseType.GetCustomAttribute(typeof(XmlClassAttribute)) as XmlClassAttribute;
			this.UseClassAttribute = useClassAttribute;
			if (this.UseClassAttribute != null)
			{
				Type typeFromHandle = typeof(IEnumerable<>);
				foreach (PropertyInfo propertyInfo in this.UseType.GetProperties())
				{
					if (propertyInfo.CanRead)
					{
						if (propertyInfo.PropertyType.IsGenericType && propertyInfo.PropertyType.GetGenericArguments().Count<Type>() == 1)
						{
							if (typeFromHandle.MakeGenericType(new Type[]
							{
								propertyInfo.PropertyType.GetGenericArguments()[0]
							}).IsAssignableFrom(propertyInfo.PropertyType))
							{
								this.LstCollectionProperty.Add(propertyInfo);
							}
						}
						else if (!propertyInfo.PropertyType.IsGenericType)
						{
							XmlPropertyAttribute xmlPropertyAttribute = propertyInfo.GetCustomAttribute(typeof(XmlPropertyAttribute)) as XmlPropertyAttribute;
							if (xmlPropertyAttribute != null)
							{
								this.UsePropertyDic.Add(propertyInfo, xmlPropertyAttribute);
							}
						}
					}
				}
			}
		}

		private Type m_useType;

		private XmlClassAttribute m_useClassAttribute;

		private Dictionary<PropertyInfo, XmlPropertyAttribute> m_usePropertyDic = new Dictionary<PropertyInfo, XmlPropertyAttribute>();

		private List<PropertyInfo> m_lstCollectionProperty = new List<PropertyInfo>();
	}
}
